import groovy.jmx.builder.JmxBuilder
import javax.management.MBeanServer
import javax.management.ObjectName

class JmxDescriptionFactoryTest extends GroovyTestCase {
    def jmx
    def objName = "jmxBuilder:type=bean"

    void testInitializtion() {
        jmx = new JmxBuilder()
        assertTrue jmx != null
    }

    void setUp() {
        jmx = new JmxBuilder()
    }

    void testJmxDescriptionFactoryExplicit() {
        def observed = new ExposedTestBean()
        MBeanServer parent = jmx.export {
            bean(observed) {
                name(objName)
                description("ThisIsADescription")
            }
        }

        assert parent != null
        assert parent.isRegistered(new ObjectName(objName))
        assert parent.getMBeanInfo(new ObjectName(objName)).getDescriptor().getFieldValue("displayName").equals("ThisIsADescription")

        parent.unregisterMBean new ObjectName(objName)
    }

    void testJmxDescriptionFactoryImplied() {
        def observed = new ExposedTestBean()
        MBeanServer parent = jmx.export {
            bean(object: observed, name: objName)
        }

        assert parent != null
        assert parent.isRegistered(new ObjectName(objName))
        assert parent.getMBeanInfo(new ObjectName(objName)).getDescriptor().getFieldValue("displayName").equals("ExposedTestBean")

        parent.unregisterMBean new ObjectName(objName)
    }

}
